package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Sections;

public class GenericSectionsDAO extends AutoKeys implements
		org.jetnuke.dao.SectionsDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericSectionsDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SectionsModel#retrieveByPK(int)
	 */
	public Sections retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("SectionModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		Sections s = new Sections();

		if (rs.next()) {
			fillSectionsFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillSectionsFromResultSet(Sections s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillSectionsFromResultSet(Sections, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setIMAGE(rs.getString("IMAGE"));
		s.setSECID(rs.getInt("SECID"));
		s.setSECNAME(rs.getString("SECNAME"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillSectionsFromResultSet(Sections, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Sections u = new Sections();
			fillSectionsFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("SectionModel.selectAll"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SectionsModel#addNew(org.jetnuke.entities.Sections)
	 */
	public int addNew(Sections sec) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Sections) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("SectionModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("SectionsModel.lastGeneratedSectionID"));
		initNewSection(sec, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Sections) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param sec
	 * @param p
	 */
	private void initNewSection(Sections sec, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewSection(Sections, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, sec.getIMAGE());
		p.setString(2, sec.getSECNAME());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewSection(Sections, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.SectionsModel#update(org.jetnuke.entities.Sections)
	 */
	public void update(Sections sc) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Sections) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("SectionModel.update"));
		p.setString(1, sc.getIMAGE());
		p.setString(2, sc.getSECNAME());
		p.setInt(3, sc.getSECID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Sections) - end"); //$NON-NLS-1$
		}
	}

}
